﻿using System;
using System.Collections.Generic;
using System.ComponentModel;
using System.Data;
using System.Drawing;
using System.Linq;
using System.Text;
using System.Windows.Forms;

namespace AppMain
{
    public partial class PreviewSwitchCfg : Form
    {
        public byte _numberOfAlternates = 1;

        RadioButton[] _alternate = new RadioButton[16];
        
        public PreviewSwitchCfg(byte numberOfAlternates)
        {
            InitializeComponent();

            _numberOfAlternates = numberOfAlternates;
            // Create Radio Buttons
            for (byte x = 0; x < _numberOfAlternates; x++)
            {
                if (x >= 16)
                {
                    break;
                }
                _alternate[x] = new RadioButton();
                _alternate[x].AutoSize = true;
                _alternate[x].Location = new System.Drawing.Point(7 + ((x * 630) / _numberOfAlternates), 438);
                _alternate[x].Name = "Alternate" + x.ToString();
                _alternate[x].Size = new System.Drawing.Size(31, 17);
                _alternate[x].TabIndex = x + 1;
                _alternate[x].TabStop = true;
                _alternate[x].Text = (x+1).ToString();
                _alternate[x].UseVisualStyleBackColor = true;
                _alternate[x].Click += new System.EventHandler(SelectAlternate);
                this.Controls.Add(_alternate[x]);
            }
            if (_alternate[0] != null)
            {
                _alternate[0].Checked = true;
            }
        }

        public void RedisplayRadioButtons(byte numberOfAlternates)
        {
            _numberOfAlternates = numberOfAlternates;
            // Modify Radio Buttons
            for (byte x = 0; x < 16; x++)
            {
                if (_alternate[x] != null)
                {
                    if (_alternate[x].Visible)
                    {
                        _alternate[x].Visible = false;
                        this.Controls.Remove(_alternate[x]);
                    }
                }
                if (x < _numberOfAlternates)
                {
                    _alternate[x] = new RadioButton();
                    _alternate[x].AutoSize = true;
                    _alternate[x].Location = new System.Drawing.Point(7 + ((x * 650) / _numberOfAlternates), 438);
                    _alternate[x].Name = "Alternate" + x.ToString();
                    _alternate[x].Size = new System.Drawing.Size(31, 17);
                    _alternate[x].TabIndex = x + 1;
                    _alternate[x].TabStop = true;
                    _alternate[x].Text = (x + 1).ToString();
                    _alternate[x].UseVisualStyleBackColor = true;
                    _alternate[x].Click += new System.EventHandler(SelectAlternate);
                    this.Controls.Add(_alternate[x]);
                }
                if (_alternate[0] != null)
                {
                    _alternate[0].Checked = true;
                }
            }
        }

        private void SelectAlternate(object sender, EventArgs e)
        {
            byte alternate = 0;
            for (byte x = 0; x < _numberOfAlternates; x++)
            {
                if (_alternate[x].Checked)
                {
                    alternate = x;
                }
            }
            bool valid = SwMatrix.UpdateWorkingSwitchConfigWithAlternate(alternate);

            if(valid)
            {
                SwMatrix._lastAlternate = alternate;
                byte[] workingTargetSwitchPositions = { 0xFF, 0xFF, 0xFF, 0xFF, 0xFF, 0xFF, 0xFF, 0xFF, 0xFF, 0xFF, 0xFF };
                for (byte x = 0; x < 11; x++)
                {
                    if (SwMatrix._workingTargetSwitchPositions[x])
                    {
                        workingTargetSwitchPositions[x] = 1;
                    }
                    else
                    {
                        workingTargetSwitchPositions[x] = 0;
                    }

                }
                SwMatrix.CalculateTransmitterFeeds(workingTargetSwitchPositions);
                // do nothing at all, since we can't seem to display a messagebox.
            }
        }
        
        private void SwitchMatrixTimer_Tick(object sender, EventArgs e)
        {
            //_txColor[0] = Color.Black;
            //_txColor[1] = Color.Magenta;
            //_txColor[2] = Color.Red;
            //_txColor[3] = Color.Black;
            //_txColor[4] = Color.Black;
            //_txColor[5] = Color.Yellow;
            //_txColor[6] = Color.Cyan;
            //_txColor[7] = Color.Blue;
            Transmitter6.BackColor = SwMatrix._txColor[6];
            Transmitter2.BackColor = SwMatrix._txColor[2];
            Transmitter1.BackColor = SwMatrix._txColor[1];
            Transmitter7.BackColor = SwMatrix._txColor[7];
            Transmitter5.BackColor = SwMatrix._txColor[5];
            TX6toSW1_1.BackColor = SwMatrix._txColor[6];
            TX6toSW1_2.BackColor = SwMatrix._txColor[6];
            TX6toSW1_3.BackColor = SwMatrix._txColor[6];
            TX2toSW1_1.BackColor = SwMatrix._txColor[2];
            TX2toSW1_2.BackColor = SwMatrix._txColor[2];
            TX1toSW2_1.BackColor = SwMatrix._txColor[1];
            TX1toSW2_2.BackColor = SwMatrix._txColor[1];
            TX1toSW2_3.BackColor = SwMatrix._txColor[1];
            TX7toSW2_1.BackColor = SwMatrix._txColor[7];
            TX7toSW2_2.BackColor = SwMatrix._txColor[7];
            TX5toSW3_1.BackColor = SwMatrix._txColor[5];
            TX5toSW3_2.BackColor = SwMatrix._txColor[5];
            // track current Switch (makes copy/paste easier)
            byte currentSwitch = 0;
            // Switch 1 Outputs
            SW1toSW4_1.BackColor = SwMatrix._txColor[SwMatrix.TransmitterFeedingSegment(currentSwitch, 0)];
            SW1toSW4_2.BackColor = SwMatrix._txColor[SwMatrix.TransmitterFeedingSegment(currentSwitch, 0)];
            SW1toSW4_3.BackColor = SwMatrix._txColor[SwMatrix.TransmitterFeedingSegment(currentSwitch, 0)];
            SW1toSW6_1.BackColor = SwMatrix._txColor[SwMatrix.TransmitterFeedingSegment(currentSwitch, 1)];
            SW1toSW6_2.BackColor = SwMatrix._txColor[SwMatrix.TransmitterFeedingSegment(currentSwitch, 1)];
            Switch1.ImageLocation = SwMatrix._imageLocations[currentSwitch++];
            Switch1.Load();
            // Switch 2 Outputs
            SW2toSW3_1.BackColor = SwMatrix._txColor[SwMatrix.TransmitterFeedingSegment(currentSwitch, 0)];
            SW2toSW3_2.BackColor = SwMatrix._txColor[SwMatrix.TransmitterFeedingSegment(currentSwitch, 0)];
            SW2toSW3_3.BackColor = SwMatrix._txColor[SwMatrix.TransmitterFeedingSegment(currentSwitch, 0)];
            SW2toSW4_1.BackColor = SwMatrix._txColor[SwMatrix.TransmitterFeedingSegment(currentSwitch, 1)];
            SW2toSW4_2.BackColor = SwMatrix._txColor[SwMatrix.TransmitterFeedingSegment(currentSwitch, 1)];
            SW2toSW4_3.BackColor = SwMatrix._txColor[SwMatrix.TransmitterFeedingSegment(currentSwitch, 1)];
            Switch2.ImageLocation = SwMatrix._imageLocations[currentSwitch++];
            Switch2.Load();
            // Switch 3 Outputs
            SW3toSW8_1.BackColor = SwMatrix._txColor[SwMatrix.TransmitterFeedingSegment(currentSwitch, 0)];
            SW3toSW8_2.BackColor = SwMatrix._txColor[SwMatrix.TransmitterFeedingSegment(currentSwitch, 0)];
            SW3toSW8_3.BackColor = SwMatrix._txColor[SwMatrix.TransmitterFeedingSegment(currentSwitch, 0)];
            SW3toSW8_4.BackColor = SwMatrix._txColor[SwMatrix.TransmitterFeedingSegment(currentSwitch, 0)];
            SW3toSW5_1.BackColor = SwMatrix._txColor[SwMatrix.TransmitterFeedingSegment(currentSwitch, 1)];
            SW3toSW5_2.BackColor = SwMatrix._txColor[SwMatrix.TransmitterFeedingSegment(currentSwitch, 1)];
            SW3toSW5_3.BackColor = SwMatrix._txColor[SwMatrix.TransmitterFeedingSegment(currentSwitch, 1)];
            Switch3.ImageLocation = SwMatrix._imageLocations[currentSwitch++];
            Switch3.Load();
            // Switch 4 Outputs
            SW4toSW5_1.BackColor = SwMatrix._txColor[SwMatrix.TransmitterFeedingSegment(currentSwitch, 0)];
            SW4toSW5_2.BackColor = SwMatrix._txColor[SwMatrix.TransmitterFeedingSegment(currentSwitch, 0)];
            SW4toSW5_3.BackColor = SwMatrix._txColor[SwMatrix.TransmitterFeedingSegment(currentSwitch, 0)];
            SW4toSW6_1.BackColor = SwMatrix._txColor[SwMatrix.TransmitterFeedingSegment(currentSwitch, 1)];
            SW4toSW6_2.BackColor = SwMatrix._txColor[SwMatrix.TransmitterFeedingSegment(currentSwitch, 1)];
            SW4toSW6_3.BackColor = SwMatrix._txColor[SwMatrix.TransmitterFeedingSegment(currentSwitch, 1)];
            Switch4.ImageLocation = SwMatrix._imageLocations[currentSwitch++];
            Switch4.Load();
            // Switch 5 Outputs
            SW5toSW8_1.BackColor = SwMatrix._txColor[SwMatrix.TransmitterFeedingSegment(currentSwitch, 0)];
            SW5toSW8_2.BackColor = SwMatrix._txColor[SwMatrix.TransmitterFeedingSegment(currentSwitch, 0)];
            SW5toSW8_3.BackColor = SwMatrix._txColor[SwMatrix.TransmitterFeedingSegment(currentSwitch, 0)];
            SW5toSW7_1.BackColor = SwMatrix._txColor[SwMatrix.TransmitterFeedingSegment(currentSwitch, 1)];
            SW5toSW7_2.BackColor = SwMatrix._txColor[SwMatrix.TransmitterFeedingSegment(currentSwitch, 1)];
            SW5toSW7_3.BackColor = SwMatrix._txColor[SwMatrix.TransmitterFeedingSegment(currentSwitch, 1)];
            Switch5.ImageLocation = SwMatrix._imageLocations[currentSwitch++];
            Switch5.Load();
            // Switch 6 Outputs
            SW6toSW7_1.BackColor = SwMatrix._txColor[SwMatrix.TransmitterFeedingSegment(currentSwitch, 0)];
            SW6toSW7_2.BackColor = SwMatrix._txColor[SwMatrix.TransmitterFeedingSegment(currentSwitch, 0)];
            SW6toSW7_3.BackColor = SwMatrix._txColor[SwMatrix.TransmitterFeedingSegment(currentSwitch, 0)];
            SW6toSW9_1.BackColor = SwMatrix._txColor[SwMatrix.TransmitterFeedingSegment(currentSwitch, 1)];
            SW6toSW9_2.BackColor = SwMatrix._txColor[SwMatrix.TransmitterFeedingSegment(currentSwitch, 1)];
            Switch6.ImageLocation = SwMatrix._imageLocations[currentSwitch++];
            Switch6.Load();
            // Switch 7 Outputs
            SW7toSW10_1.BackColor = SwMatrix._txColor[SwMatrix.TransmitterFeedingSegment(currentSwitch, 0)];
            SW7toSW10_2.BackColor = SwMatrix._txColor[SwMatrix.TransmitterFeedingSegment(currentSwitch, 0)];
            SW7toSW10_3.BackColor = SwMatrix._txColor[SwMatrix.TransmitterFeedingSegment(currentSwitch, 0)];
            SW7toSW9_1.BackColor = SwMatrix._txColor[SwMatrix.TransmitterFeedingSegment(currentSwitch, 1)];
            SW7toSW9_2.BackColor = SwMatrix._txColor[SwMatrix.TransmitterFeedingSegment(currentSwitch, 1)];
            SW7toSW9_3.BackColor = SwMatrix._txColor[SwMatrix.TransmitterFeedingSegment(currentSwitch, 1)];
            Switch7.ImageLocation = SwMatrix._imageLocations[currentSwitch++];
            Switch7.Load();
            // Switch 8 Outputs
            SW8toANT4_1.BackColor = SwMatrix._txColor[SwMatrix.TransmitterFeedingSegment(currentSwitch, 0)];
            SW8toANT4_2.BackColor = SwMatrix._txColor[SwMatrix.TransmitterFeedingSegment(currentSwitch, 0)];
            SW8toANT4_3.BackColor = SwMatrix._txColor[SwMatrix.TransmitterFeedingSegment(currentSwitch, 0)];
            SW8toSW11_1.BackColor = SwMatrix._txColor[SwMatrix.TransmitterFeedingSegment(currentSwitch, 1)];
            SW8toSW11_2.BackColor = SwMatrix._txColor[SwMatrix.TransmitterFeedingSegment(currentSwitch, 1)];
            SW8toSW11_3.BackColor = SwMatrix._txColor[SwMatrix.TransmitterFeedingSegment(currentSwitch, 1)];
            Switch8.ImageLocation = SwMatrix._imageLocations[currentSwitch++];
            Switch8.Load();
            // Switch 9 Outputs
            SW9toANT2A_1.BackColor = SwMatrix._txColor[SwMatrix.TransmitterFeedingSegment(currentSwitch, 0)];
            SW9toANT2A_2.BackColor = SwMatrix._txColor[SwMatrix.TransmitterFeedingSegment(currentSwitch, 0)];
            SW9toANT2A_3.BackColor = SwMatrix._txColor[SwMatrix.TransmitterFeedingSegment(currentSwitch, 0)];
            SW9toANT2A_4.BackColor = SwMatrix._txColor[SwMatrix.TransmitterFeedingSegment(currentSwitch, 0)];
            SW9toANT2A_5.BackColor = SwMatrix._txColor[SwMatrix.TransmitterFeedingSegment(currentSwitch, 0)];
            SW9toANT1_1.BackColor = SwMatrix._txColor[SwMatrix.TransmitterFeedingSegment(currentSwitch, 1)];
            SW9toANT1_2.BackColor = SwMatrix._txColor[SwMatrix.TransmitterFeedingSegment(currentSwitch, 1)];
            Switch9.ImageLocation = SwMatrix._imageLocations[currentSwitch++];
            Switch9.Load();
            // Switch 10 Outputs
            SW10toLOAD_1.BackColor = SwMatrix._txColor[SwMatrix.TransmitterFeedingSegment(currentSwitch, 0)];
            SW10toLOAD_2.BackColor = SwMatrix._txColor[SwMatrix.TransmitterFeedingSegment(currentSwitch, 0)];
            SW10toLOAD_3.BackColor = SwMatrix._txColor[SwMatrix.TransmitterFeedingSegment(currentSwitch, 0)];
            SW10toLOAD_4.BackColor = SwMatrix._txColor[SwMatrix.TransmitterFeedingSegment(currentSwitch, 0)];
            SW10toLOAD_5.BackColor = SwMatrix._txColor[SwMatrix.TransmitterFeedingSegment(currentSwitch, 0)];
            SW10toANT3_1.BackColor = SwMatrix._txColor[SwMatrix.TransmitterFeedingSegment(currentSwitch, 1)];
            SW10toANT3_2.BackColor = SwMatrix._txColor[SwMatrix.TransmitterFeedingSegment(currentSwitch, 1)];
            Switch10.ImageLocation = SwMatrix._imageLocations[currentSwitch++];
            Switch10.Load();
            // Switch 11 Outputs
            SW11toANT2B_1.BackColor = SwMatrix._txColor[SwMatrix.TransmitterFeedingSegment(currentSwitch, 0)];
            SW11toANT2B_2.BackColor = SwMatrix._txColor[SwMatrix.TransmitterFeedingSegment(currentSwitch, 0)];
            SW11toANT2B_3.BackColor = SwMatrix._txColor[SwMatrix.TransmitterFeedingSegment(currentSwitch, 0)];
            SW11toANT5_1.BackColor = SwMatrix._txColor[SwMatrix.TransmitterFeedingSegment(currentSwitch, 1)];
            SW11toANT5_2.BackColor = SwMatrix._txColor[SwMatrix.TransmitterFeedingSegment(currentSwitch, 1)];
            Switch11.ImageLocation = SwMatrix._imageLocations[currentSwitch++];
            Switch11.Load();
            // Antennas
            Antenna1.BackColor =  SwMatrix._txColor[SwMatrix.TransmitterFeedingSegment(8, 1)];
            Antenna2A.BackColor = SwMatrix._txColor[SwMatrix.TransmitterFeedingSegment(8, 0)];
            Antenna3.BackColor =  SwMatrix._txColor[SwMatrix.TransmitterFeedingSegment(9, 1)];
            DummyLoad.BackColor = SwMatrix._txColor[SwMatrix.TransmitterFeedingSegment(9, 0)];
            Antenna5.BackColor =  SwMatrix._txColor[SwMatrix.TransmitterFeedingSegment(10, 1)];
            Antenna2B.BackColor = SwMatrix._txColor[SwMatrix.TransmitterFeedingSegment(10, 0)];
            Antenna4.BackColor =  SwMatrix._txColor[SwMatrix.TransmitterFeedingSegment(7, 0)];
            ConfigCost.Text = SwMatrix._configCost.ToString();
        }
    }
}
